{extend name="layout/admin" /}

{block name="body"}
<div class="layui-card-body">
    <form class="layui-form" action="">
        <div class="layui-input-inline layui-show-xs-block padding-bottom-10">
            <input class="layui-input" placeholder="用户名" name="username">
        </div>
        <div class="layui-input-inline layui-show-xs-block padding-bottom-10">
            <select name="status">
                <option value="">状态</option>
                <option value="1">成功</option>
                <option value="0">失败</option>
            </select>
        </div>
        <div class="layui-input-inline layui-show-xs-block padding-bottom-10">
            <button class="layui-btn" lay-submit="" lay-filter="sreach">
                <i class="layui-icon">&#xe615;</i>搜索
            </button>
        </div>
    </form>
    <hr>
    <table id="loginLog" lay-filter="loginLog"></table>
</div>
<script>
  layui.use(['table', 'form'], function () {
    var table = layui.table
    var form = layui.form

    //初始化表格
    table.render({
      elem: '#loginLog',
      method: 'post',
      url: '/admin/loginLog/index',
      page: true,
      cols: [[
        { field: 'id', title: 'ID' },
        { field: 'username', title: '登录用户名' },
        {
          field: 'type', title: '登录类型', templet: function (d) {
            return LogTypeFormat(d.type)
          },
        },
        { field: 'ip', title: '登录IP' },
        {
          field: 'status', title: '登录状态', templet: function (d) {
            return StatusFormatHTML(d.status)
          },
        },
        { field: 'create_time', title: '登录时间' },
        { field: 'update_time', title: '修改时间' },
      ]],
      parseData: function (res) {
        return {
          'code': res.code,
          'msg': res.message,
          'count': res.data.count,
          'data': res.data.lists,
        }
      },
    })

    //监听表格事件
    table.on('tool(loginLog)', function (obj) {
      var data = obj.data
      var layEvent = obj.event

      if (layEvent === 'del') {
        layer.confirm('确认删除此行数据吗？', function (index) {
          layer.close(index)
          $.post('/admin/loginLog/delete/' + data.id, {}, function (result) {
            layer.msg(result.message)
            if (result.code === 0) {
              obj.del()
            }
          })
        })
      } else if (layEvent === 'edit') {
        xadmin.open('编辑角色', '/admin/loginLog/update/' + data.id)
      }
    })

    //搜索
    form.on('submit(sreach)', function (data) {
      table.reload('loginLog', {
        where: data.field,
        page: {
          curr: 1, //重新从第 1 页开始
        },
      })
      return false
    })
  })
</script>
{/block}
